package com.grapevine.contact.util;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;

public class ContactPhoto {
	public static Bitmap getPhoto(long people_id, Context context) {
		String photo_id = null;

		String[] projection1 = new String[] { ContactsContract.Contacts.PHOTO_ID };
		String selection1 = ContactsContract.Contacts._ID + " = " + people_id;
		Cursor cur1 = context.getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI, projection1, selection1,
				null, null);
		if (cur1.moveToFirst()) {
			photo_id = cur1.getString(0);
		}
		cur1.close();
		if (null == photo_id) {
			return null;
		}
		String[] projection = new String[] { ContactsContract.Data.DATA15 };
		String selection = "ContactsContract.Data._ID = " + photo_id;
		Cursor cur = context.getContentResolver().query(
				ContactsContract.Data.CONTENT_URI, projection, selection, null,
				null);
		Bitmap bitmap = null;
		if (cur.moveToFirst()) {
			byte[] contactIcon = cur.getBlob(0);

//			Log.e("SIZE", "w = " + width);
//			Log.e("SIZE", "h = " + height);
			// Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
			// bytes.length, null);
			 bitmap = BitmapFactory.decodeByteArray(contactIcon, 0,
			 contactIcon.length);
//			bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(
//					contactIcon, 0, contactIcon.length, null), width, height,
//					true);
		}
		cur.close();
		return bitmap;
	}
}
